package com.xinyun.traceability.enity.dto;

import com.github.pagehelper.PageInfo;
import lombok.Data;

import java.util.List;

/**
 * 分页dto
 */
@Data
public class PageQueryResult<T> {
    private String msg;
    private String code;
    private boolean success;
    private Long count;
    private List data;

    public PageQueryResult() {
        this(null);
    }

    public PageQueryResult(T result) {
        this(result, "");
    }

    public PageQueryResult(T result, String msg) {
        this(result, msg, true);
    }

    public PageQueryResult(T result, String msg, boolean isSuccess) {
        this(result, msg, isSuccess, "0");
    }

    public PageQueryResult(T result, String msg, boolean isSuccess, String code) {
        this.code = code;
        this.msg = msg;
        this.success = isSuccess;
        if (result instanceof PageInfo) {
            this.count = ((PageInfo) result).getTotal();
            this.data = ((PageInfo) result).getList();
        } else if (result instanceof List) {
            this.data = (List) result;
        }
    }


    public List getData() {
        return data;
    }

    /**
     * 设置data，如果传入的是list，则需要手动设置count（数据总条数）
     *
     * @param data 可接受的值：List or  Page&lt;List&gt;
     */
    public void setData(List<T> data) {
        if (data instanceof PageInfo) {
            this.count = ((PageInfo) data).getTotal();
            this.data = ((PageInfo) data).getList();
        } else if (data instanceof List) {
            this.data = (List) data;
        }
    }
}
